﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Helper
/// </summary>
public class Helper
{
	public Helper()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static int getQuery(string paramater)
    {
        int result = 0;
        try
        {
            string tmp = "";
            tmp = System.Web.HttpContext.Current.Request.QueryString[paramater];
            if (tmp != null && !tmp.Equals(""))
            {
                result = int.Parse(tmp);
            }
        }
        catch (Exception ex)
        {
            result = 0;
            throw new Exception(ex.Message);
        }
        return result;
    }

    public static string getQuerystr(string paramater)
    {
        string tmp = "";
        try
        {
            tmp = System.Web.HttpContext.Current.Request.QueryString[paramater];
        }
        catch (Exception ex)
        {
            tmp = "";
            throw new Exception(ex.Message);
        }
        return tmp;
    }

    public static bool logIn()
    {
        UserEntities item = (UserEntities)System.Web.HttpContext.Current.Session["user"];
        if (item != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public static void bindDropdown(DropDownList dl, string[] values)
    {
        for (int i = 0; i < values.Length; i++)
        {
            dl.Items.Add(new ListItem(values[i], i.ToString()));
        }
    }

    public static void bindDropdown1(DropDownList dl, string[] value_name, string[] value_url)
    {
        for (int i = 0; i < value_name.Length; i++)
        {
            dl.Items.Add(new ListItem(value_name[i], value_url[i]));
        }
    }

    public static string EncodePass(string str)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider encode = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] pass = System.Text.Encoding.UTF8.GetBytes(str);
        pass = encode.ComputeHash(pass);
        System.Text.StringBuilder build = new System.Text.StringBuilder();
        foreach (byte p in pass)
        {
            build.Append(p.ToString());
        }
        return build.ToString();
    }

    public static void ViewImage(string tablename, string filename, int id)
    {
        try
        {
            System.Web.HttpResponse respone = System.Web.HttpContext.Current.Response;
            string sql = "SELECT " + filename + " FROM " + tablename + " WHERE countryID='" + id + "'";
            SqlDataReader readimage = Database.getReader(sql);
            if (readimage.Read())
            {
                respone.ContentType = "image/jpeg";
                respone.BinaryWrite((byte[])readimage[filename]);
            }
            readimage.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}